import sys
import time
import requests
from lxml.html import etree


# 1.拿到页面源代码
kw = input("请输入您希望搜索的内容：")
root = "https://so.gushiwen.cn"
search = (root + f"/search.aspx?value={kw}&valuej={kw[0]}").encode("utf-8")
print("正在访问：", search)

resp = requests.get(
    url=search,
    headers={"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36", "referer": search},
)
content = resp.text
print("页面源代码获取成功！")


# 2.解析页面源代码
print("正在解析数据。")
tree = etree.HTML(content)

best_result = tree.xpath("""/html/body/div[2]/div[1]/div[3]/div[1]/p[1]/a""")[0]
best_name = ''.join(best_result.xpath(""".//*/text()""")).strip()
href = root + best_result.xpath("""./@href""")[0]

results = tree.xpath("""/html/body/div[2]/div[1]/div[@class="sons"]""")
resultsLIST = []
for result in results:
    name = ''.join(result.xpath("""./div[1]/p[1]/a//*/text()""")).strip()
    author = ''.join([i.strip() for i in result.xpath("""./div[1]/p[@class="source"]//*/text()""")][0:1])
    # article = result.xpath("""./div[1]/div[2]/p[1]/text()""")[0].strip()
    # article = result.xpath("""./div[1]/div[2]/p[1]""")[0]
    href = root + result.xpath("""./div[1]/p[1]/a/@href""")[0]
    # resultsLIST.append({"name": name, "author": author, "article": article, "href": href})
    resultsLIST.append({"name": name, "author": author, "href": href})
amount = len(resultsLIST)

print("数据解析成功！")


# 3.拿到最符合的网址
if best_name == kw:
    print("成功搜索到与 %s 的最佳匹配项！" % kw)
else:
    for it in resultsLIST:
        print("\t".join(it.values()))
    ans = input("未找到最佳匹配项，我们搜索到以上几项内容，请输入您想要的编号。（跳过请输入0）")
    for i in range(10):
        if ans.isdigit():
            if int(ans) == 0:
                print("正在退出程序。")
                time.sleep(1)
                sys.exit()
            elif 1 <= int(ans) <= amount:
                break  #########
            else:
                print("请输入数字编号或0。")
        else:
            print("请输入数字。")
        ans = input("您的输入不合法，请重新输入。")


# 4.从子页面中获取有用信息。
contentS = ''
treeS = etree.HTML(contentS)  # treeS意为son_tree
